<?php
/*
 * @Author: GeYang
 * @Date: 2020-11-02 08:34:20
 * @LastEditTime: 2021-04-22 10:35:52
 * @LastEditors: Please set LastEditors
 * @Description: 水电管理model类
 * @FilePath: \application\models\Cost\ApplicationForm_model.php
 */
// 水电管理model类
class ApplicationForm_model extends CI_Model
{
    // 构造方法
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    /**
     * @description: 新增免扣销假数据
     * @param {data}
     * @return {*}
     */    
    public function create($data = FALSE)
    {

        if($data === FALSE){

            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        }

        $result = $this->db->insert('dorm_leave_off', $data);

        if( $this->db->affected_rows() === 1)
        {

            return json_encode(array('res' => '40000', 'msg' => '操作成功', 'data' =>''));

        }else{

            return json_encode(array('res' => '40001', 'msg' => '操作失败,请重试', 'data' =>''));

        }


    }

    /**
     * @description: 查询免扣销假记录表
     * @param {data, page}
     * @return {*}
     */    
    public function search($data = FALSE, $page = FALSE)
    {
            $nameSql = $data['name'] == "" ? " 1 = 1 " :  " name LIKE '%".$data['name']."%' ";

            $jobNumberSql = $data['jobNumber'] == "" ? " AND 1 = 1 " :  "  AND jobNumber LIKE '%".$data['jobNumber']."%' ";
    
            $room_number = $data['room_number'] == "" ? " AND  1 = 1 " :  " AND  room_number LIKE '%".$data['room_number']."%' ";
    
            $beginDatebe = $data['apply_beginDate'] == "" ? " AND 1 = 1 " :  " AND apply_beginDate >= ".$data['apply_beginDate']." ";
    
            $beginDateEnd = $data['apply_endDate'] == "" ? " AND 1 = 1 " :  " AND apply_endDate <= ".$data['apply_endDate']." ";
    
            $endDateBe = $data['endDateBe'] == "" ? " AND 1 = 1 " :  " AND apply_endDate <= ".$data['endDateBe']." ";
    
            $endDateEnd = $data['endDateEnd'] == "" ? " AND 1 = 1 " :  " AND apply_endDate >= ".$data['endDateEnd']." ";
    
            $apply_type = $data['apply_type'] == "" ? " AND 1 = 1 " :  " AND apply_type = ".$data['apply_type']." ";

            // $nameSql = $data['name'] == "" ? " 1 = 1 " :  " name LIKE '%".$data['name']."%' ";

            // $jobNumberSql = $data['jobNumber'] == "" ? " AND 1 = 1 " :  "  AND jobNumber LIKE '%".$data['jobNumber']."%' ";
    
            // $room_number = $data['room_number'] == "" ? " AND  1 = 1 " :  " AND  room_number LIKE '%".$data['room_number']."%' ";
    
            // $beginDatebe = $data['apply_beginDate'] == "" ? " AND 1 = 1 " :  " AND apply_beginDate >= ".$data['apply_beginDate']." ";
    
            // $beginDateEnd = $data['apply_endDate'] == "" ? " AND 1 = 1 " :  " AND apply_endDate <= ".$data['apply_endDate']." ";
            
            // $apply_type = $data['apply_type'] == "" ? " AND 1 = 1 " :  " AND apply_type = ".$data['apply_type']." ";
            

        $query = $this->db->query("SELECT * FROM dorm_leave_off WHERE ".$nameSql.$jobNumberSql.$room_number.$beginDatebe.$beginDateEnd.$endDateBe.$endDateEnd.$apply_type." ORDER BY ID ");
        $totalPage = $query->num_rows();
        // $query = $this->db->query("SELECT * FROM dorm_leave_off WHERE ".$nameSql.$jobNumberSql.$room_number.$beginDatebe.$beginDateEnd.$apply_type." ORDER BY ID ");
        // $totalPage = $query->num_rows();

        if($page !== FALSE)
        {
            $query = $this->db->query("SELECT * FROM dorm_leave_off WHERE ".$nameSql.$jobNumberSql.$room_number.$beginDatebe.$beginDateEnd.$endDateBe.$endDateEnd.$apply_type." ORDER BY ID LIMIT ".$page." ,10 ");
            // $query = $this->db->query("SELECT * FROM dorm_leave_off WHERE ".$nameSql.$jobNumberSql.$room_number.$beginDatebe.$beginDateEnd.$apply_type." ORDER BY ID LIMIT ".$page." ,10 ");
        }

        $resultData = $query->result();
        
        if($totalPage === 0)
        {
            return json_encode(array('res' => '40002', 'msg' => '查询无记录', 'data' => ''));
        }
        elseif($totalPage > 0)
        {
            $result = array('data' => $resultData, 'totalPage' => $totalPage);
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $result ));
        }
            
    }

    /**
     * @description: 新增房间水电记录表数据
     * @param {date, area_number, floor_number, dorm_floor, room_number, room_hotwater, room_coldwater, room_electric, type}
     * @return {*}
     */    
    // public function saveRoomMeter($roomMeterArray = FALSE)
    public function saveRoomMeter($date = FALSE, $area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $room_number = FALSE, $room_hotwater = FALSE, $room_coldwater = FALSE, $room_electric = FALSE, $type = FALSE)
    {

        // if($roomMeterArray === FALSE)
        // {

        //     return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        // }
        if($date === FALSE || $area_number === FALSE || $floor_number === FALSE || $dorm_floor === FALSE || $room_number === FALSE || $room_hotwater === FALSE || $room_coldwater === FALSE || $room_electric === FALSE || $type === FALSE)
        {

            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        }

        // $insertArray = array(
        //     'date' => $roomMeterArray['date'],
        //     'area_number' => $roomMeterArray['area_number'],
        //     'floor_number' => $roomMeterArray['floor_number'],
        //     'dorm_floor' => $roomMeterArray['dorm_floor'],
        //     'room_number' => $roomMeterArray['room_number'],
        //     'room_hotwater' => $roomMeterArray['room_hotwater'],
        //     'room_coldwater' => $roomMeterArray['room_coldwater'],
        //     'room_electric' => $roomMeterArray['room_electric'],
        //     'type' => $roomMeterArray['type']
        // );

        $insertArray = array(
            'date' => $date,
            'area_number' => $area_number,
            'floor_number' => $floor_number,
            'dorm_floor' => $dorm_floor,
            'room_number' => $room_number,
            'room_hotwater' => $room_hotwater,
            'room_coldwater' => $room_coldwater,
            'room_electric' => $room_electric,
            'type' => $type
        );

        // 插入数据
        $query = $this->db->insert('dorm_room_hydropower', $insertArray);

        if($this->db->affected_rows() === 1)
        {

            return json_encode(array('res' => '40000', 'msg' => '操作成功', 'data' =>''));

        }else{

            return json_encode(array('res' => '40001', 'msg' => '操作失败', 'data' =>''));

        }


    }

    /**
     * @description: 查询房间水电记录表数据
     * @param {area_number, area_number, dorm_floor, room_number, monthBegin, monthEnd, type}
     * @return {*}
     */    
    public function getRoomMeter($area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $room_number = FALSE, $monthBegin = FALSE, $monthEnd = FALSE, $type = FALSE)
    {
        if($area_number === FALSE || $floor_number === FALSE || $dorm_floor === FALSE || $room_number === FALSE || $monthBegin === FALSE || $monthEnd === FALSE || $type === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }

        $searchArray = array(
                'area_number' => $area_number,
                'floor_number' => $floor_number,
                'dorm_floor' => $dorm_floor,
                'room_number' => $room_number,
                'date >=' => $monthBegin,
                'date <=' => $monthEnd,
                'type' => $type
            );
        
        $query = $this->db->get_where('dorm_room_hydropower', $searchArray);
        if($query->num_rows() > 0)
        {

            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row() ));

        }else{

            return json_encode(array('res' => '40002', 'msg' => '查询无记录', 'data' => ''));

        }
    }
    
    /**
     * @description: 取上月均摊数据
     * @param {area_number, floor_number, dorm_floor, date}
     * @return {*}
     */    
    // public function getCapitation($area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $year = FALSE, $month = FALSE)
    public function getCapitation($area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $date = FALSE)
    {
        //核算月上月第一天
        $monthBegin = date("Y-m-01", (strtotime("-1 month", strtotime($date))));;

        //核算月上月最后一天
        $monthEnd = date("Y-m-t", (strtotime("-1 month", strtotime($date))));

        if($area_number === FALSE || $floor_number === FALSE || $dorm_floor === FALSE || $date === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }

        $searchArray = array(
            // 'year' => $year,
            // 'month' => $month,
            'date >=' => $monthBegin,
            'date <=' => $monthEnd,
            'area_number' => $area_number,
            'floor_number' => $floor_number,
            'dorm_floor' => $dorm_floor
        );

        $query = $this->db->get_where('dorm_coefficient', $searchArray);

        if($query->num_rows() > 0)
        {

            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row() ));

        }else{

            return json_encode(array('res' => '40002', 'msg' => '查询无记录', 'data' => ''));

        }

    }

    // public function calcHotWaterUnitPrice($date = FALSE, $area_number = FALSE, $dorm_floor = FALSE)
    // {

    //     if($date === FALSE || $area_number === FALSE || $floor_number === FALSE)
    //     {

    //         return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

    //     }

    //     $searchArr = array(
    //         'public_date' => $date,
    //         'area_number' => $area_number,
    //         'floor_number' => $floor_number,
    //     );

    //     $query = $this->db->get_where('dorm_public_hydropower', $searchArr);

    //     if( $query->num_rows() === 0 )
    //     {

    //         return json_encode(array('res' => '40000', 'msg' => '无数据', 'data' =>''));

    //     }else{

    //         return json_encode(array('res' => '40000', 'msg' => '无数据', 'data' => $query->row()));

    //     }
    // }

    /**
     * @description: 拉取入住日期
     * @param {jobNumber, idNumber}
     * @return {*}
     */    
    public function liveDay($jobNumber = FALSE, $idNumber = FALSE)
    {
        // $liveDate = $this->db->query("SELECT liveDate FROM dorm_stay_retreat_tune WHERE jobNumber = '".$jobNumber."' ORDER BY liveDate DESC");
        $liveDate = $this->db->query("SELECT liveDate FROM dorm_stay_retreat_tune WHERE jobNumber = '".$jobNumber."' OR idNumber = '".$idNumber."' ORDER BY liveDate DESC");
        
        if($liveDate->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $liveDate->row()));
        }
        else
        {
            // $liveDate = $this->db->query("SELECT liveDate FROM dorm_livepeople WHERE jobNumber = '".$jobNumber."'");
            $liveDate = $this->db->query("SELECT liveDate FROM dorm_livepeople WHERE jobNumber = '".$jobNumber."' OR idNumber = '".$idNumber."'");
            if($liveDate->num_rows()>0)
            {
                return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $liveDate->row()));
            }
            else
            {
                return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            }
        }
    }

    /**
     * @description: 拉取请假单
     * @param {jobNumber,date}
     * @return {*}
     */    
    public function apply($jobNumber, $date)
    {
        $dateFirst = date("Y-m-01");
        $applyDays = $this->db->query("SELECT apply_days FROM dorm_leave_off WHERE jobNumber = '".$jobNumber."' AND apply_type = 0 AND apply_endDate >= '".$dateFirst."' AND apply_beginDate <= $date");
        if($applyDays ->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $applyDays->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 拉取销假单
     * @param {jobNumber,date}
     * @return {*}
     */    
    public function leaveOff($jobNumber, $date)
    {
        $dateFirst = date("Y-m-01");
        $leaveOff = $this->db->query("SELECT apply_days FROM dorm_leave_off WHERE jobNumber = '".$jobNumber."' AND apply_type = 1 AND apply_endDate >= '".$dateFirst."' AND apply_beginDate <= $date");
        if($leaveOff->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $leaveOff->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 拉取房间入住人员
     * @param {area_number, floor_number, dorm_floor, room_number}
     * @return {*}
     */    
    public function room_livepeople($area_number, $floor_number, $dorm_floor ,$room_number)
    {
        $query_livepeople = $this->db->get_where('dorm_bed', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'room_number' => $room_number, 'check_inWhether' => '1'));
        if($query_livepeople->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query_livepeople->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 拉取水电人天表
     * @param {jobNumber}
     * @return {*}
     */    
    public function manday($jobNumber)
    {
        $manday = $this->db->get_where('dorm_manday_calc' ,array('jobNumber' => $jobNumber));
        if($manday->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $manday->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }

    }

    /**
     * @description: 拉取退宿人员费用
     * @param {area_number, floor_number, dorm_floor, room_number, date}
     * @return {*}
     */    
    public function retreatCost($area_number, $floor_number, $dorm_floor, $room_number,$date)
    {
        $date_first = date("Y-m-01", strtotime($date));
        $cost= $this->db->query("SELECT * FROM dorm_hydropwer_cost WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND dorm_floor = '".$dorm_floor."' AND room_number = '".$room_number."' AND type = 2 AND cost_date BETWEEN '".$date_first."' AND '".$date."' ");
 
        if($cost->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $cost->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description:拉取换宿人员费用 
     * @param {area_number, floor_number, dorm_floor, room_number, date}
     * @return {*}
     */    
    public function tuneinCost($area_number, $floor_number, $dorm_floor, $room_number,$date)
    {
        $date_first = date("Y-m-01");
        $cost = $this->db->query("SELECT * FROM dorm_hydropwer_cost WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND dorm_floor = '".$dorm_floor."' AND room_number = '".$room_number."' AND type = 3 AND cost_date BETWEEN '".$date_first."' AND '".$date."'");
        if($cost->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $cost->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 根据工号确定入住位置
     * @param {jobNumber, idNumber}
     * @return {*}
     */    
    public function liveAddress($jobNumber = FALSE, $idNumber = FALSE)
    {
        // $liveAddress = $this->db->get_where('dorm_bed', array('jobNumber' => $jobNumber, 'check_inWhether' => 1));
        $liveAddress = $this->db->query("SELECT * FROM dorm_bed WHERE jobNumber = '".$jobNumber."' OR idNumber = '".$idNumber."' AND check_inWhether = 1");
        ;
        if($liveAddress->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $liveAddress->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 保存个人费用
     * @param {singleCostData}
     * @return {*}
     */    
    public function saveSingleFee($singleCostData = FALSE)
    {

        if( $singleCostData === FALSE )
        {

            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        }

        $query = $this->db->insert('dorm_hydropwer_cost', $singleCostData);
        if($this->db->affected_rows() === 1)
        {

            return json_encode(array('res' => '40000', 'msg' => '操作成功', 'data' =>''));

        }else{

            return json_encode(array('res' => '40001', 'msg' => '操作失败', 'data' =>''));

        }

    }

    /**
     * @description: 水电费明细查询总条数
     * @param {jobNumber, name, room_number, beginDate, endDate}
     * @return {*}
     */    
    public function mandayDetail_all($jobNumber, $name, $room_number, $beginDate, $endDate)
    {
        $sj1 = $jobNumber == '' ? " 1 = 1 " : " jobNumber LIKE '%".$jobNumber."%'";
        $sj2 = $name == '' ? " AND 1 = 1 " : " AND name LIKE '%".$name."%'";
        $sj3 = $room_number == '' ? " AND 1 = 1 " : " AND room_number LIKE '%".$room_number."%'";
        // $sj4 = "cost_date BETWEEN '".$beginDate."' AND '".$endDate."'";
        $sj4 = $sj4 = "AND cost_date >='".$beginDate."' AND cost_date <='".$endDate."'";
        if($beginDate == FALSE || $endDate == FALSE)
        {
            //无开始结束日期
            $sql = "SELECT A.cost_date, A.area_number, A.floor_number, A.dorm_floor, A.room_number, A.jobNumber, B.name,  A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll 
                    FROM dorm_hydropwer_cost AS A LEFT JOIN dorm_livepeople AS B ON A.jobNumber = B.jobNumber WHERE" .$sj1 .$sj2 .$sj3;
            $detail = $this->db->query($sql, $sj1, $sj2, $sj3);
        }
        else
        {
            //无开始结束日期
            $sql = "SELECT A.cost_date, A.area_number, A.floor_number, A.dorm_floor, A.room_number, A.jobNumber, B.name,  A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll 
                    FROM dorm_hydropwer_cost AS A LEFT JOIN dorm_livepeople AS B ON A.jobNumber = B.jobNumber WHERE" .$sj1 .$sj2 .$sj3 .$sj4;
            $detail = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4);

        }
        if($detail->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $detail->num_rows()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 水电费明细查询
     * @param {jobNumber, name, room_number, beginDate, endDate, limit, totalpage}
     * @return {*}
     */    
    public function mandayDetail($jobNumber, $name, $room_number, $beginDate, $endDate, $limit, $totalpage)
    {
        if($limit != FALSE)
        {
            $limit1 = ("$limit" - 1) * 10;
        }
        $sj1 = $jobNumber == '' ? " 1 = 1 " : " jobNumber LIKE '%".$jobNumber."%'";
        $sj2 = $name == '' ? " AND 1 = 1 " : " AND name LIKE '%".$name."%'";
        $sj3 = $room_number == '' ? " AND 1 = 1 " : " AND room_number LIKE '%".$room_number."%'";
        $sj4 = "AND cost_date BETWEEN '".$beginDate."' AND '".$endDate."'";
        $sj5 = $limit == '' ? ' AND 1 = 1 ' : "Limit " . $limit1 . ",10"; 
        if($limit != '')
        {
            if($beginDate == FALSE || $endDate == FALSE)
            {
                //无开始结束日期
                $sql = "SELECT A.cost_date, A.area_number, A.floor_number, A.dorm_floor, A.room_number, A.jobNumber, C.name, A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll, A.type, B.area_name, B.floor_name
                        FROM dorm_hydropwer_cost AS A LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number 
                        LEFT JOIN dorm_livepeople AS C ON A.jobNumber = C.jobNumber
                        WHERE" .$sj1 .$sj2 .$sj3 .$sj5;
                $detail = $this->db->query($sql, $sj1, $sj2, $sj3, $sj5);
            }
            else
            {
                //有开始结束日期
                $sql = "SELECT A.cost_date, A.area_number, A.floor_number, A.dorm_floor, A.room_number, A.jobNumber, C.name, A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll, A.type, B.area_name, B.floor_name
                        FROM dorm_hydropwer_cost AS A LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number 
                        LEFT JOIN dorm_livepeople AS C ON A.jobNumber = C.jobNumber
                        WHERE" .$sj1 .$sj2 .$sj3 .$sj4 .$sj5;
                $detail = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4, $sj5);
            }
        }
        else
        {
            if($beginDate == FALSE  || $endDate == FALSE)
            {
                //有开始结束日期
                $sql = "SELECT A.cost_date, A.area_number, A.floor_number, A.dorm_floor, A.room_number, A.jobNumber, C.name, A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll, A.type, B.area_name, B.floor_name
                        FROM dorm_hydropwer_cost AS A LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number 
                        LEFT JOIN dorm_livepeople AS C ON A.jobNumber = C.jobNumber
                        WHERE" .$sj1 .$sj2 .$sj3;
                $detail = $this->db->query($sql, $sj1, $sj2, $sj3);
            }
            else
            {
                //无开始结束日期
                $sql = "SELECT A.cost_date, A.area_number, A.floor_number, A.dorm_floor, A.room_number, A.jobNumber, C.name, A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll, A.type, B.area_name, B.floor_name
                        FROM dorm_hydropwer_cost AS A LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number 
                        LEFT JOIN dorm_livepeople AS C ON A.jobNumber = C.jobNumber
                        WHERE" .$sj1 .$sj2 .$sj3 .$sj4;;
                $detail = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4);
            }
        }
        if($detail->num_rows()>0)
        {
            $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $detail->result(), 'page' => $totalpage);
            $result = json_encode($return_su);
        }
        else
        {
            $return_fa = array('res' => '40001', 'msg' => '查询无数据', 'data' => '');
            $result = json_encode($return_fa);
        }
        return $result;
    }


    //水电费用全查询
    public function monthmandayDetail($beginDate, $endDate)
    {
        $query = $this->db->get_where('dorm_hydropwer_cost', array('cost_date>=' => $beginDate, 'cost_date<=' => $endDate));
        if($query)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));  
        }
        else
        {
            return json_encode(array('res' => '40002', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /** 
    * @Purpose: 
    * 计算各楼层房间水电表总和
    * @Method Name: SumRoomMeter
    * 
    * @Param
    *       string area_number 区域编号
    *       string floor_number 宿舍楼编号
    *       string dorm_floor 宿舍楼层号
    *       date   beginDate 开始时间
    *       date   endDate  结束时间
    *       date   type     类型
    *    
    * @Author: GeYang
    *
    * @Return: array cost(
    *       string area_number 区域编号
    *       string floor_number 宿舍楼编号
    *       string dorm_floor 宿舍楼层号
    *       number sumColdWater 冷水总和
    *       number sumWaterHot 热水总和
    *       number sumElectricty 电表总和
    *            ) 
    */

    public function SumRoomMeter($area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $beginDate = FALSE, $endDate = FALSE, $type = FALSE)
    {

        if($area_number === FALSE || $floor_number === FALSE || $dorm_floor === FALSE || $beginDate === FALSE || $endDate === FALSE || $type === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }
        
        $query = $this->db->query(" SELECT area_number, floor_number, dorm_floor, SUM(room_hotwater) AS sumWaterHot, SUM(room_coldwater) AS sumColdWater, SUM(room_electric) AS sumElectricty FROM dorm_room_hydropower WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND dorm_floor = '".$dorm_floor."' AND date BETWEEN '".$beginDate."' AND '".$endDate."' AND type = '".$type."' GROUP BY area_number, floor_number, dorm_floor ");

        if($query->num_rows() > 0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无记录', 'data' => ''));
        }

    }

    //计算宿舍水电表总和
    public function SumRoom($area_number = FALSE, $floor_number = FALSE, $beginDate = FALSE, $endDate = FALSE, $type = FALSE)
    {

        if($area_number === FALSE || $floor_number === FALSE || $beginDate === FALSE || $endDate === FALSE || $type === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }
        
        $query = $this->db->query(" SELECT area_number, floor_number, SUM(room_hotwater) AS sumWaterHot, SUM(room_coldwater) AS sumColdWater, SUM(room_electric) AS sumElectricty FROM dorm_room_hydropower WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND date BETWEEN '".$beginDate."' AND '".$endDate."' AND type = '".$type."' GROUP BY area_number, floor_number");

        if($query->num_rows() > 0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无记录', 'data' => ''));
        }

    }

    // 统计各楼层总人天
    /** 
    * @Purpose: 
    * 统计各楼层总人天
    * @Method Name: 
    * 
    * @Param
    *       string area_number 区域编号
    *       string floor_number 宿舍楼编号
    *       string dorm_floor 宿舍楼层号
    *       date   beginDate 开始时间
    *       date   endDate  结束时间
    *    
    * @Author: GeYang
    *
    * @Return: array cost(
    *       string area_number 区域编号
    *       string floor_number 宿舍楼编号
    *       string dorm_floor 宿舍楼层号
    *       number sumDays 人天
    *            ) 
    */
    public function SumFloorDays($area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $beginDate = FALSE, $endDate = FALSE)
    {

        if($area_number === FALSE || $floor_number === FALSE || $dorm_floor === FALSE || $beginDate === FALSE || $endDate === FALSE)
        {

            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        }

        $query = $this->db->query("SELECT area_number, floor_number, dorm_floor, SUM(liveDays) AS sumLiveDays FROM dorm_manday WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND dorm_floor = '".$dorm_floor."' AND date BETWEEN '".$beginDate."' AND '".$endDate."' AND type = '0' GROUP BY area_number, floor_number, dorm_floor ");

        if($query->num_rows() > 0)
        {

            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row() ));

        }else{

            return json_encode(array('res' => '40002', 'msg' => '查询无记录', 'data' => ''));

        }
    }

    //宿舍总人天
    public function SumDays($area_number = FALSE, $floor_number = FALSE, $beginDate = FALSE, $endDate = FALSE)
    {

        if($area_number === FALSE || $floor_number === FALSE || $beginDate === FALSE || $endDate === FALSE)
        {

            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        }

        $query = $this->db->query("SELECT area_number, floor_number, SUM(liveDays) AS sumLiveDays FROM dorm_manday WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND date BETWEEN '".$beginDate."' AND '".$endDate."' AND type = '0' GROUP BY area_number, floor_number");

        if($query->num_rows() > 0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row() ));
        }
        else
        {
            return json_encode(array('res' => '40002', 'msg' => '查询无记录', 'data' => ''));
        }
    }
    
    /**
     * @description: 保存人天表方法
     * @param {manday}
     * @return {*}
     */    
    public function savemanday($manday)
    {
        $this->db->insert('dorm_manday', $manday);
        $query_manday = $this->db->affected_rows();
        if($query_manday)
        {
            $return_su = array('res' => '40000', 'msg' => '更新成功', 'data' => '');
            $result = json_encode($return_su);
        }
        else
        {
            $return_fa = array('res' => '40001', 'msg' => '更新失败', 'data' => '');
            $result = json_encode($return_fa);
        }
        return $result;
    }

    /**
     * @description:A/B栋 333上月需负担费用 
     * @param {area_number, floor_number, date}
     * @return {*}
     */    
    public function lastMonth($area_number, $floor_number, $date)
    {
        $month = substr($date, 5, 2) - 1;
        $monthFirst = date("Y-$lastMonth-01");
        $monthLast = date("Y-$lastMonth-t");
        $days = strtotime($monthLast) - strtotime($monthFirst);
        $person_coldwater = $this->db->get_where('dorm_hydropwer_cost', array('area_number' => $area_number, 'floor_number' => $floor_number, 'cost_date >=' => $monthFirst, 'cost_date <=' => $monthLast, 'month_dayall' => $days));
        if($person_coldwater->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $person_coldwater->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 新增楼层水电表
     * @param {floorMeter}
     * @return {*}
     */    
    public function insertFloorMeter($floorMeter = FALSE)
    {

        if($floorMeter === FALSE)
        {

            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        }

        $deleteArray = array(
            'public_date' => $floorMeter[0],
            'area_number' => $floorMeter[1],
            'floor_number' => $floorMeter[2],
            'dorm_floor' => $floorMeter[3],
        );

        $this->db->delete('dorm_public_hydropower', $deleteArray);

        $insertArray = array(
            'public_date' => $floorMeter[0],
            'area_number' => $floorMeter[1],
            'floor_number' => $floorMeter[2],
            'dorm_floor' => $floorMeter[3],
            'airElectricity' => $floorMeter[4],
            'airWater' => $floorMeter[5],
            'floor_coldwater' => $floorMeter[6],
            'floor_electricty' => $floorMeter[7],
            'washingMachine' => $floorMeter[8],
            'floor_dayall' => $floorMeter[9],
            'dorm_totalElectricty' => $floorMeter[10],
            'dorm_totalWater' => $floorMeter[11],
        );

        $result = $this->db->insert('dorm_public_hydropower', $insertArray);

        if( $this->db->affected_rows() === 1)
        {

            return json_encode(array('res' => '40000', 'msg' => '操作成功', 'data' =>''));

        }else{

            return json_encode(array('res' => '40001', 'msg' => '操作失败,请重试', 'data' =>''));

        }

    }

    /**
     * @description: 新增房间水电表
     * @param {roomMeter}
     * @return {*}
     */    
    public function insertRoomMeter($roomMeter = FALSE)
    {

        if($roomMeter === FALSE)
        {

            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));

        }

        $deleteArray = array(
            'date' => (string)$roomMeter[0],
            'area_number' => (string)$roomMeter[1],
            'floor_number' => (string)$roomMeter[2],
            'dorm_floor' => (string)$roomMeter[3],
            'room_number' => (string)$roomMeter[4],
        );

        $this->db->delete('dorm_room_hydropower', $deleteArray);

        $insertArray = array(
            'date' => $roomMeter[0],
            'area_number' => $roomMeter[1],
            'floor_number' => $roomMeter[2],
            'dorm_floor' => $roomMeter[3],
            'room_number' => $roomMeter[4],
            'room_hotwater' => $roomMeter[5],
            'room_coldwater' => $roomMeter[6],
            'room_electric' => $roomMeter[7],
            'type' => $roomMeter[8],
        );

        $result = $this->db->insert('dorm_room_hydropower', $insertArray);
        

        if( $this->db->affected_rows() === 1)
        {

            return json_encode(array('res' => '40000', 'msg' => '操作成功', 'data' =>''));

        }else{

            return json_encode(array('res' => '40001', 'msg' => '操作失败,请重试', 'data' =>''));

        }

    }

    /**
     * @description: 获取楼层表——需输入楼层号
     * @param {area_number, floor_number, dorm_floor, beginDate, endDate}
     * @return {*}
     */
    public function searchFloorMeter($area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $beginDate = FALSE, $endDate = FALSE)
    {
        if($area_number === FALSE || $floor_number === FALSE || $dorm_floor === FALSE || $beginDate === FALSE ||  $endDate === FALSE )
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }
        else
        {
            $searchArr = array(
                'area_number' => $area_number,
                'floor_number' => $floor_number,
                'dorm_floor' => $dorm_floor,
                'public_date >= ' => $beginDate,
                'public_date <= ' => $endDate
            );   
        }

        $query = $this->db->get_where('dorm_public_hydropower', $searchArr );
        if($query->num_rows() > 0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }

    }

    /**
     * @description: 获取楼层表——不必输入楼层号
     * @param {area_number, floor_number, beginDate, endDate}
     * @return {*}
     */
    public function searchFloor($area_number = FALSE, $floor_number = FALSE, $beginDate = FALSE, $endDate = FALSE)
    {
        if($area_number === FALSE || $floor_number === FALSE || $beginDate === FALSE ||  $endDate === FALSE )
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }
        else
        {
            $searchArr = array(
                'area_number' => $area_number,
                'floor_number' => $floor_number,
                'public_date >= ' => $beginDate,
                'public_date <= ' => $endDate
            );   
        }

        $query = $this->db->get_where('dorm_public_hydropower', $searchArr );
        if($query->num_rows() > 0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }

    }
    
    /**
     * @description: 获取178公共数据
     * @param {area_number, floor_number, beginDate, endDate}
     * @return {*}
     */
    public function searchFloorMeter_178($area_number = FALSE, $floor_number = FALSE, $beginDate = FALSE, $endDate = FALSE)
    {
        if($area_number === FALSE || $floor_number === FALSE || $beginDate === FALSE ||  $endDate === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }
        else
        {
            $searchArr = array(
                'area_number' => $area_number,
                'floor_number' => $floor_number,
                // 'dorm_floor' => $dorm_floor,
                'public_date >= ' => $beginDate,
                'public_date <= ' => $endDate
            );
        }

        $query = $this->db->get_where('dorm_public_hydropower', $searchArr);

        if($query->num_rows() > 0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无记录', 'data' => ''));
        }
        
    }

    /**
     * @description: 取出宿舍楼所有入住人员
     * @param {*}
     * @return {*}
     */
    public function livePeople()
    {
        $query = $this->db->get_where('dorm_bed', array('check_inWhether' => '1'));
        if($query)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description:公共费用存入表 
     * @param {arr}
     * @return {*}
     */
    public function public_hydropower($arr)
    {
        for($i=0 ;$i<count($arr); $i++)
        {
            $this->db->insert('dorm_coefficient', $arr[$i]);
        }
        $query = $this->db->affected_rows();
        if($query)
        {
            return json_encode(array('res' => '40000', 'msg' => '操作成功', 'data' => ''));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '操作失败', 'data' => ''));
        }
    }

    /**
     * @description: 取所有房间总人天
     * @param {beginDate, endDate}
     * @return {*}
     */
    public function roomManday($beginDate, $endDate)
    {   
        $query = $this->db->query("SELECT area_number, floor_number, dorm_floor, room_number, SUM(liveDays) AS sumliveDays FROM dorm_manday WHERE date BETWEEN '".$beginDate."' AND '".$endDate."' AND type = '0' GROUP BY area_number floor_number dorm_floor room_number");
        if($query->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description:取按房间取总人天 
     * @param {area_number, floor_number, dorm_floor, room_number, beginDate, endDate}
     * @return {*}
     */
    public function room_Manday($area_number, $floor_number, $dorm_floor, $room_number, $beginDate, $endDate)
    {   
        $query = $this->db->query("SELECT area_number, floor_number, dorm_floor, room_number, SUM(liveDays) AS sumliveDays FROM dorm_manday WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND dorm_floor = '".$dorm_floor."' AND room_number = '".$room_number."' AND date BETWEEN '".$beginDate."' AND '".$endDate."' AND type = '0' GROUP BY area_number, floor_number, dorm_floor, room_number");
        if($query->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            
        }
    }

    /**
     * @description: 取人员人天
     * @param {jobNumber, beginDate, endDate}
     * @return {*}
     */
    public function month_manday($jobNumber, $beginDate, $endDate)
    {
        $query = $this->db->get_where('dorm_manday', array('jobNumber' => $jobNumber, 'date >=' => $beginDate, 'date <=' => $endDate));
        if($query)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }
    
    /**
     * @description:取公摊费用 
     * @param {beginDate, endDate, area_number, floor_number, dorm_floor}
     * @return {*}
     */
    public function publicprice($beginDate, $endDate, $area_number, $floor_number, $dorm_floor)
    {
        $query=$this->db->get_where('dorm_publicprice', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'date>=' => $beginDate, 'date<=' => $endDate));
        if($query)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }

    /**
     * @description: 数据保存到费用表
     * @param {arr}
     * @return {*}
     */
    public function hydropwerCost($arr)
    {
        $query = $this->db->insert_batch('dorm_hydropwer_cost', $arr);
        if($query)
        {
            return json_encode(array('res' => '40000', 'msg' => '操作成功', 'data' => ''));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '操作失败', 'data' => ''));
        }
    }

    /**
     * @description: 取电表度数
     * @param {area_number, floor_number, dorm_floor, room_number, beginDate, endDate}
     * @return {*}
     */
    public function electric($area_number = FALSE, $floor_number = FALSE, $dorm_floor = FALSE, $room_number = FALSE, $beginDate = FALSE, $endDate = FALSE)
    {
        
        if($area_number === FALSE || $floor_number === FALSE || $dorm_floor === FALSE || $room_number === FALSE || $beginDate=== FALSE || $endDate === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }
        else
        {
            $query = $this->db->get_where('dorm_room_hydropower', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'room_number' => $room_number, 'date >=' => $beginDate, 'date <=' => $endDate));
            if($query->num_rows()>0)
            {
                return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
            }
            else
            {
                return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            }
        }
        
    }

    /**
     * @description: 取333 空气能用水总和
     * @param {area_number, floor_number, beginDate, endDate}
     * @return {*}
     */
    public function sumairelectric($area_number = FALSE, $floor_number = FALSE, $beginDate = FALSE, $endDate = FALSE)
    {
        if($area_number === FALSE || $floor_number === FALSE || $beginDate === FALSE || $endDate === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>'')); 
        }
        
        $query = $this->db->query("SELECT area_number, floor_number, SUM(airWater) AS airWater FROM dorm_public_hydropower WHERE area_number = '".$area_number."' AND floor_number = '".$floor_number."' AND public_date BETWEEN '".$beginDate."' AND '".$endDate."' GROUP BY area_number, floor_number");
        if($query->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }
    }
    
    /**
     * @description:取热水费单价 
     * @param {area_number, floor_number, date}
     * @return {*}
     */
    public function hotwater_price($area_number = FALSE, $floor_number = FALSE, $date = FALSE)
    {
        //核算月第一天
        $monthBegin = date("Y-m-01", strtotime($date));;

        //核算月最后一天
        $monthEnd = date("Y-m-t", strtotime($date));

        if($area_number === FALSE || $floor_number === FALSE || $date === FALSE)
        {
            return json_encode(array('res' => '41001', 'msg' => '数据异常', 'data' =>''));
        }

        $searchArray = array(
            // 'year' => $year,
            // 'month' => $month,
            'date >=' => $monthBegin,
            'date <=' => $monthEnd,
            'area_number' => $area_number,
            'floor_number' => $floor_number,
        );

        $query = $this->db->get_where('dorm_coefficient', $searchArray);

        if($query->num_rows() > 0)
        {

            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row() ));

        }else{

            return json_encode(array('res' => '40002', 'msg' => '查询无记录', 'data' => ''));

        }

    }

    /**
     * @description: 个人费用取出
     * @param {date}
     * @return {*}
     */
    public function personCost($date = FALSE)
    {
        if($date === FALSE)
        {
            $query = $this->db->query("SELECT A.cost_date, B.area_name, B.floor_name, A.dorm_floor, A.room_number, A.jobNumber, C.name, A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll FROM dorm_hydropwer_cost AS A 
                                        LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number 
                                        LEFT JOIN dorm_livepeople AS C ON A.jobNumber = C.jobNumber
                                        WHERE type = 0");
        }
        else
        {
            //核算月第一天
            $monthBegin = date("Y-m-01", strtotime($date));

            //核算月最后一天
            $monthEnd = date("Y-m-t", strtotime($date));

            $query = $this->db->query("SELECT A.cost_date, B.area_name, B.floor_name, A.dorm_floor, A.room_number, A.jobNumber, C.name, A.month_dayall, A.person_hotwater, A.person_coldwater, A.person_electric, A.public_hotwater, A.public_coldwater, A.public_electric, A.costAll FROM dorm_hydropwer_cost AS A 
                                        LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number 
                                        LEFT JOIN dorm_livepeople AS C ON A.jobNumber = C.jobNumber 
                                        WHERE cost_date BETWEEN '".$monthBegin."' AND '".$monthEnd."' AND type = 0");           
        }
                
        $result = $query->result();

        return json_encode(array('res' => '40000', 'msg' => $result));
    }

    /**
     * @description: 房间水电表数据取出
     * @param {date}
     * @return {*}
     */
    public function waterMeter($date = FALSE)
    {
        if($date === FALSE)
        {
            $query = $this->db->query("SELECT A.date, B.area_name, B.floor_name, A.floor_number, A.dorm_floor, A.room_number, A.room_hotwater, A.room_coldwater, A.room_electric, A.type FROM dorm_room_hydropower AS A
                                        LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number");
            
        }
        else
        {
            //核算月第一天
            $monthBegin = date("Y-m-01", strtotime($date));

            //核算月最后一天
            $monthEnd = date("Y-m-t", strtotime($date));
            
            $query = $this->db->query("SELECT A.date, B.area_name, B.floor_name, A.floor_number, A.dorm_floor, A.room_number, A.room_hotwater, A.room_coldwater, A.room_electric, A.type FROM dorm_room_hydropower AS A
                                        LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number WHERE date BETWEEN '".$monthBegin."' AND '".$monthEnd."'");
            
        }
        $result = $query->result();

        return json_encode(array('res' => '40000', 'msg' => $result));
    }

    /**
     * @description:水电表房间数据取出 
     * @param {date}
     * @return {*}
     */
    public function publicCost($date = FALSE)
    {
        if($date === FALSE)
        {
            $query = $this->db->query("SELECT A.public_date, B.area_name, B.floor_name, A.floor_number, A.dorm_floor, A.airElectricity, A.airWater, A.floor_coldwater, A.floor_electricty, A.washingMachine, A.floor_dayall, A.dorm_totalElectricty, A.dorm_totalWater FROM dorm_public_hydropower AS A
                                        LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number");
        }
        else
        {
            //核算月第一天
            $monthBegin = date("Y-m-01", strtotime($date));

            //核算月最后一天
            $monthEnd = date("Y-m-t", strtotime($date));
            
            $query = $this->db->query("SELECT A.public_date, B.area_name, B.floor_name, A.floor_number, A.dorm_floor, A.airElectricity, A.airWater, A.floor_coldwater, A.floor_electricty, A.washingMachine, A.floor_dayall, A.dorm_totalElectricty, A.dorm_totalWater FROM dorm_public_hydropower AS A
                                        LEFT JOIN dorm_sslmanage AS B ON A.area_number = B.area_number AND A.floor_number = B.floor_number WHERE public_date BETWEEN '".$monthBegin."' AND '".$monthEnd."'");
            
        }
        $result = $query->result();

        return json_encode(array('res' => '40000', 'msg' => $result));
    }

    /**
     * @description:已经结算过的月份不允许重新结算
     * @param {date}
     * @return {*}
     */
    public function CostControl($date = FALSE)
    {
        //核算月第一天
        $monthBegin = date("Y-m-01", strtotime($date));

        //核算月最后一天
        $monthEnd = date("Y-m-t", strtotime($date));
        
        if($date === FALSE)
        {
            return json_encode(array('res' => '40002', 'msg' => '数据异常', 'data' => ''));   
        }
        else
        {
            $query = $this->db->query("SELECT * FROM dorm_hydropwer_cost WHERE cost_date BETWEEN '".$monthBegin."' AND '".$monthEnd."'"); 
        }
        if($query->num_rows()>0)
        {
            return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));
        }
        else
        {
            return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }

    }
}

?>